package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass=UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    /**
     * 根据用户名找到用户
     * @param username
     * @return
     *   目的：封装user
     */
    @Override
    public User findUserByUsername(String username) {
//        1. 先查看用户基本信息--封装基本信息
       User userInDb= userDao.findUserByUsername(username);
       Integer userId=userInDb.getId();
//        2. 查看用户与角色--把role封装到user
        Set<Role> roles=roleDao.findRoleById(userId);
//        3. 查看角色与权限--把permession封装到role
        if (roles.size()>0 && roles!=null){
            for (Role role:roles){
                Integer roleId=role.getId();
                Set<Permission> permissions= permissionDao.findPermissionById(roleId);
                if (permissions!=null && permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
            userInDb.setRoles(roles);
        }
        return  userInDb;
    }
}
